<template>
  <div>
    <van-nav-bar
      title="编辑信息"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />

    <van-field
      v-model="name"
      label="昵称"
      :placeholder="getInfo.name"
      input-align="right"
    />
    <van-field
      v-model="age"
      label="年龄"
      :placeholder="getInfo.age"
      input-align="right"
    />
    <van-field
      v-model="gender"
      label="性别"
      :placeholder="Gender"
      input-align="right"
    />
    <van-cell title="手机号" :value="getInfo.phone" />
    <van-button type="primary" block @click="onSubmit"> 保存 </van-button>
  </div>
</template>
<script>
import Url from "../tools/Url";
import { Toast } from "mint-ui";
export default {
  name: "my",
  data() {
    return {
      name: "",
      gender: "",
      age: "",
      phone: "",
      password: "",
    };
  },
  computed: {
    getInfo() {
      return this.$store.state.info;
    },
    Gender() {
      var gender;
      if (this.$store.state.info.gender == 1) {
        gender = "男";
      } else {
        gender = "女";
      }
      return gender;
    },
  },
  methods: {
    genders() {
      var genders;
      if (this.gender == "男") {
        genders = 1;
      } else {
        genders = 2;
      }
      return genders;
    },
    onSubmit() {
      var genders = this.genders();
      var token = sessionStorage.getItem("token");
      var str = `token=${token}&id=${this.getInfo.id}&age=${this.age}&gender=${genders}&name=${this.name}`;
      this.axios.post(Url.SHEZHI_URL, str).then((res) => {
        console.log("保存成功");
        console.log(res);
        if (res.data.code == 200) {
          Toast(res.data.message);
          this.$store.commit("saveInfo", res.data.data);
          console.log("ok");
          this.$router.push("/page");
        } else {
          Toast(res.data.message);
        }
      });
    },
    onClickLeft() {
      this.$router.push("/page");
    },
  },
};
</script>
